home *** CD-ROM | disk | FTP | other *** search
/ Programmers Heaven 2 / Programmers Heaven 2.iso / files / graphics / library / wgt51_r2.zip / WGT5 / EXAMPLES / WGT59.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-03  |  13.4 KB  |  501 lines

  1. /*
  2. ==============================================================================
  3.               WordUp Graphics Toolkit Version 5.0                     
  4.                  Demonstration Program 59                         
  5.                                           
  6.  This is a PACMAN clone which has two player mode.                            
  7.                                           
  8.  The right player uses the arrow keys, and the left player uses               
  9.  the W, A, S, and D keys.                                                     
  10.  
  11.  *** PROJECT ***                                                             
  12.  This program requires the files WGT5_WC.LIB and WSCR_WC.LIB to be linked.   
  13.                                           
  14.  *** DATA FILES ***                                                          
  15.  PACSPR.SPR, PACTILE.SPR, PACMAN.WMP                                         
  16.                                WATCOM C++ VERSION 
  17. ==============================================================================
  18. */
  19.  
  20. #include <dos.h>
  21. #include <malloc.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <conio.h>
  25. #include <wgt5.h>
  26. #include <wgtscrol.h>
  27.  
  28. #define LEFT 0
  29. #define RIGHT 1
  30. #define UP 2
  31. #define DOWN 3
  32.  
  33. #define ESC 1
  34. #define WIN 0
  35. #define WIN2 1
  36.  
  37. short keys[2][4] = {{30, 32, 17, 31}, {75, 77, 72, 80}};
  38.  
  39. #define NUM_LOAD 100
  40. #define DIESPEED 40
  41.  
  42. int frames;                        /* Number of frames counted */
  43. short spx,spy;                     /* Speed of scrolling window */
  44. short pacanim, pacdir[2];          /* Controls animation frame and the direction
  45.                       PACMAN is facing */
  46. short ox[2],oy[2];                 /* Stores PACMAN's coordinate from previous
  47.                       frame, in case you hit a wall */
  48. wgtmap pacmap;                     /* our world map */
  49. short dying[2];                    /* Counts down after dying.  Once it hits 0,
  50.                       PACMAN starts a new life. */
  51. color palette[256];                /* Our palette of colours */
  52. block pactile[NUM_LOAD + 1];       /* Tiles for the map */
  53. block pacspr[NUM_LOAD + 1];        /* Our sprites */
  54. scrollsprite wobject[100];         /* A few objects for you, ghosts, etc */
  55. short tiletypes[256];              /* Tile types */
  56.  
  57. short pacspeed = 4,ghostspeed = 4; /* Moving speed of you and the ghosts */
  58. short oldmode;                     /* Previous video mode */
  59.  
  60. short bonus;                       /* Tells how many points eating a ghost will 
  61.                       give you  0 = 100, 1 = 200, 2 = 400, 
  62.                       3 = 800 */
  63. short traptime[6];                 /* Time left until ghost reappears */
  64. short moved[6], movedir[6];        /* Controls movement of ghosts */
  65. short bluetime;                    /* Time left with blue ghosts */
  66. short i;
  67. short score[2];                    /* Your score */
  68. short level;                       /* Current level */
  69. short bonusscore[5] = {100, 200, 400, 800}; /* Bonus points for each ghost */
  70. short total_dots;                  /* Dots not eaten */
  71. int timer;
  72.  
  73. void checkpos (short);             /* checks PACMAN for hitting walls */
  74. void moveghost (short);            /* checks ghosts for hitting walls */
  75.  
  76. short oldmode;
  77.  
  78.  
  79. void timerctr (void)
  80. {
  81.   timer++;
  82. }
  83.  
  84.  
  85. void set_positions (void)
  86. /* Sets the initial positions of the ghosts and pacman */
  87. {
  88.   short i;
  89.  
  90.   wobject[0].on = 1;             /* Set Pacman's position */
  91.   wobject[0].x = 192; 
  92.   wobject[0].y = 176;
  93.   wobject[0].num = 1;
  94.  
  95.   wobject[1].on = 1;             /* Set Pacman's position */
  96.   wobject[1].x = 208; 
  97.   wobject[1].y = 176;
  98.   wobject[1].num = 1;
  99.  
  100.   for (i = 2; i < 6; i++)
  101.   {
  102.     wobject[i].on = 1;
  103.     wobject[i].x = 176;  /* Set up the ghosts */
  104.     wobject[i].y = 112; 
  105.     wobject[i].num = 19 + i;  /* Make the ghosts different colors */
  106.   }
  107.  
  108.   for (i = 2; i < 6; i++) 
  109.   {
  110.     moved[i] = 16;
  111.     movedir[i] = 4;
  112.   }
  113.   pacanim = 1;
  114. }
  115.  
  116.  
  117. void count_dots(void)
  118. {
  119.   unsigned short *mapptr;
  120.   int size, i;
  121.  
  122.   mapptr = (unsigned short *)pacmap;
  123.   total_dots = 0;
  124.  
  125.   for (i = 0; i < mapwidth[WIN] * mapheight[WIN]; i++)
  126.   {
  127.     if ((tiletypes[*mapptr] == 2) || (tiletypes[*mapptr] == 3))
  128.       total_dots++;
  129.     mapptr++;
  130.   }
  131. }
  132.  
  133.  
  134.  
  135. void incscore(short player, short amt)
  136. /* Increases your score by amt and shows the change on the screen. */
  137. {
  138.   score[player] += amt;
  139.   wgtprintf (10 + player * 160, 192, NULL, "%i", score[player]);
  140. }
  141.  
  142.  
  143.  
  144. void check_dots_left (void)
  145. /* Checks how many dots are left. */
  146. {
  147.   if (total_dots == 0)
  148.   {
  149.     nosound ();
  150.     wfreemap (pacmap);
  151.     pacmap = wloadmap (WIN, "pacman.wmp", tiletypes, wobject);
  152.     set_positions ();
  153.     wshowwindow (WIN, 5, 5);
  154.     wshowwindow (WIN2, 6, 5);
  155.     count_dots ();
  156.     ghostspeed *= 2;
  157.     if (ghostspeed > 16)
  158.       ghostspeed = 16;
  159.   }
  160. }
  161.  
  162.  
  163.  
  164. void checkpos (short player)
  165. /* Checks your position, and makes sure you don't go through a wall */
  166. {
  167.   short hit = 0;
  168.   short nx;
  169.  
  170.   i = wgetworldblock (player, wobject[player].x, wobject[player].y);
  171.   if (tiletypes[i] == 0)
  172.     hit = 1;
  173.   i = wgetworldblock (player, wobject[player].x+15, wobject[player].y);
  174.   if (tiletypes[i] == 0)
  175.     hit = 1;
  176.   i = wgetworldblock (player, wobject[player].x, wobject[player].y + 15);
  177.   if ((tiletypes[i] == 0) | (tiletypes[i] == 4))
  178.     hit = 1;
  179.   i = wgetworldblock (player, wobject[player].x + 15, wobject[player].y + 15);
  180.   if (tiletypes[i] == 0)
  181.     hit = 1;
  182.   i = wgetworldblock (player, wobject[player].x + 7, wobject[player].y + 7);
  183.   if (tiletypes[i] == 2)
  184.   {
  185.     sound (500);
  186.     wputworldblock (WIN, wobject[player].x + 7, wobject[player].y + 7, 12);
  187.     incscore (player, 10);
  188.     total_dots--;
  189.     check_dots_left ();
  190.   }
  191.   if (tiletypes[i] == 3)
  192.   {
  193.     sound (800);
  194.     wputworldblock (WIN, wobject[player].x + 7, wobject[player].y + 7, 12);
  195.     bluetime += 150;
  196.     total_dots--;
  197.     check_dots_left ();
  198.   }
  199.   if (wobject[player].x < 4)
  200.   {
  201.     wobject[player].x = mapwidth[WIN] * 16 - 32;
  202.     wshowwindow (player, (worldx[player] + 144) / 16, worldy[player] / 16);
  203.   }
  204.   if (wobject[player].x > mapwidth[WIN] * 16 - 17)
  205.   {
  206.     wobject[player].x = 16;
  207.     wshowwindow (player, 0, worldy[player] / 16);
  208.   }
  209.   if (hit == 1)
  210.   {
  211.     wobject[player].x = ox[player];
  212.     wobject[player].y = oy[player];
  213.     moved[player] = 0;
  214.   }
  215. }
  216.  
  217.  
  218.  
  219. void moveghost (short numb)
  220. /* Checks the ghost's position, and makes sure it doesn't go through a wall */
  221. {
  222.   short gox, goy;          /* Coordinate of ghost */
  223.   short hit = 0;
  224.   short j;
  225.  
  226.   gox = wobject[numb].x;
  227.   goy = wobject[numb].y;
  228.  
  229.   if (moved[numb] == 0)
  230.     movedir[numb] = (rand () % 5) +1; /* Pick a random direction (1-5)  5 = still */
  231.  
  232.   switch (movedir[numb])
  233.   {
  234.     case 1: wobject[numb].x += ghostspeed; 
  235.         break;
  236.     case 2: wobject[numb].x -= ghostspeed; 
  237.         break;
  238.     case 3: wobject[numb].y += ghostspeed; 
  239.         break;
  240.     case 4: wobject[numb].y -= ghostspeed; 
  241.         break;
  242.   }
  243.  
  244.   moved[numb] += ghostspeed;
  245.   if (moved[numb] >= 16)
  246.     moved[numb] = 0;
  247.  
  248.   /* Make sure ghost doesn't move off the screen. */
  249.   if (wobject[numb].x < 0)
  250.     wobject[numb].x = 0;
  251.   else if (wobject[numb].x > (mapwidth[WIN] - 1) * 16)
  252.     wobject[numb].x = (mapwidth[WIN] - 1) * 16;
  253.  
  254.   if (wobject[numb].y < 0)
  255.     wobject[numb].y = 0;
  256.   else if (wobject[numb].y > (mapheight[WIN] - 1) * 16)
  257.     wobject[numb].y = (mapheight[WIN] - 1) * 16;
  258.  
  259.   j = wgetworldblock (WIN, wobject[numb].x, wobject[numb].y);
  260.   if (tiletypes[j] == 0)
  261.     hit = 1;
  262.   j = wgetworldblock (WIN, wobject[numb].x + 15, wobject[numb].y);
  263.   if (tiletypes[j] == 0)
  264.     hit = 1;
  265.   j = wgetworldblock(WIN, wobject[numb].x, wobject[numb].y + 15);
  266.   if (tiletypes[j] == 0)
  267.     hit = 1;
  268.   if ((tiletypes[j] == 4) & (movedir[numb] == 3))  /* Can't move down through
  269.                               ghost's door */
  270.     hit = 1;
  271.   j = wgetworldblock(WIN, wobject[numb].x + 14, wobject[numb].y + 15);
  272.   if (tiletypes[j] == 0)
  273.     hit = 1;
  274.   
  275.   if (hit == 1)  /* A solid tile was run into */
  276.   {
  277.     wobject[numb].x = gox; /* Set to the old position */
  278.     wobject[numb].y = goy;
  279.     moved[numb] = 0;
  280.   }
  281.   if ((wobject[numb].x == gox) && (wobject[numb].y == goy))
  282.     moveghost (numb);    /* Recursive call, makes sure ghost moves in a new
  283.                 direction if it ran into a wall the first time. */
  284. }
  285.  
  286.  
  287. void do_player (short player)
  288. {
  289.   spx = 0;
  290.   spy = 0;
  291.   ox[player] = wobject[player].x;
  292.   oy[player] = wobject[player].y;
  293.  
  294.   if (dying[player] == 0)
  295.   {
  296.     switch(movedir[player])
  297.     {
  298.       case 1: wobject[player].x += pacspeed; 
  299.           break;
  300.       case 2: wobject[player].x -= pacspeed; 
  301.           break;
  302.       case 3: wobject[player].y += pacspeed; 
  303.           break;
  304.       case 4: wobject[player].y -= pacspeed; 
  305.           break;
  306.     }
  307.     if (movedir[player] != 0)
  308.     {
  309.       moved[player] += pacspeed;
  310.       checkpos (player);
  311.     }
  312.     if (moved[player] >= 16)
  313.       moved[player] = 0;
  314.     if (moved[player] == 0)
  315.     {
  316.       if (kbdon[keys[player][RIGHT]])
  317.       {
  318.     movedir[player] = 1;
  319.     pacdir[player] = 0;
  320.       }
  321.       else if (kbdon[keys[player][LEFT]])
  322.       {
  323.     movedir[player] = 2;
  324.     pacdir[player] = 2;
  325.       }
  326.       else if (kbdon[keys[player][UP]])
  327.       {
  328.     movedir[player] = 4;
  329.     pacdir[player] = 1;
  330.       }
  331.       else if (kbdon[keys[player][DOWN]])
  332.       {
  333.     movedir[player] = 3;
  334.     pacdir[player] = 3;
  335.       }
  336.     }
  337.     pacanim++;
  338.     if (pacanim > 5)
  339.       pacanim = 1;
  340.     wobject[player].num = pacanim + (pacdir[player] * 5);
  341.     /* Set the sprite image for pacman */
  342.   }
  343.   else  /* Dying loop */
  344.   {
  345.     wobject[player].num = (DIESPEED - dying[player] + 31);
  346.     if (wobject[player].num > 34) 
  347.       wobject[player].num = 34;
  348.     dying[player]--;
  349.     if (dying[player] == 0)
  350.     {
  351.       wobject[player].x = 192; 
  352.       wobject[player].y = 176;
  353.       wobject[player].num = 1;
  354.     }
  355.   }   
  356. }
  357.  
  358.  
  359. void main(void)
  360. {
  361.   short j;
  362.  
  363.   oldmode = wgetmode ();
  364.   if (!vgadetected ())
  365.   {
  366.     printf ("VGA is required to run this program...");
  367.     exit (1);
  368.   }
  369.  
  370.   printf ("WGT Example #59\n\n");
  371.   printf ("This is a PACMAN clone which has two player mode.\n");
  372.   printf ("The right player uses the arrow keys, and the left player uses\n");
  373.   printf ("the W, A, S, and D keys.\n");
  374.   printf ("\nPress any key to begin\n");
  375.   getch ();
  376.  
  377.   vga256 ();
  378.  
  379.   wtextcolor (1);
  380.   wtexttransparent (TEXTFGBG);
  381.   wtextbackground (254);
  382.  
  383.   /* Load the graphics */
  384.   wloadsprites (palette, "pactile.spr", pactile, 1, NUM_LOAD);
  385.   wloadsprites (palette, "pacspr.spr", pacspr, 1, NUM_LOAD);
  386.   wsetpalette (0, 255, palette);
  387.  
  388.   /* load our pacman map */
  389.   winitscroll (WIN, NORMAL, -1, 10, 12, pactile);
  390.   winitscroll (WIN2, NORMAL, -1, 10, 12, pactile);
  391.   pacmap = wloadmap (WIN, "pacman.wmp", tiletypes, wobject);
  392.   wcopymap (WIN, WIN2);
  393.  
  394.   wcls (0);
  395.   wbutt (0, 0, 319, 199);
  396.  
  397.   wshowwindow (WIN, 5, 5);
  398.   wshowwindow (WIN2, 6, 5);
  399.   count_dots ();
  400.  
  401.   installkbd ();
  402.  
  403.   set_positions ();
  404.  
  405.   frames = 0;
  406.   timer = 0;
  407.   winittimer ();
  408.   wstarttimer (timerctr, TICKS(100));   /* Program runs at top speed */
  409.   do {
  410.     do_player (0);
  411.     do_player (1);
  412.  
  413.     if (bluetime > 0)
  414.       bluetime--;
  415.  
  416.     for (i = 2; i < 6; i++)              /* Ghost loop */
  417.     {
  418.       if (traptime[i] == 0)
  419.       {
  420.     moveghost (i);
  421.     if (bluetime > 0)
  422.       wobject[i].num = 25;
  423.     if ((bluetime == 1) || ((bluetime < 40) && (bluetime % 2 == 1)))
  424.       wobject[i].num = 19 + i;
  425.  
  426.     for (j = 0; j < 2; j++)
  427.       if ((soverlap (i, wobject, pacspr, j, wobject, pacspr) == 1) && 
  428.           (traptime[i] == 0))
  429.     {
  430.       if (bluetime > 0)
  431.       {
  432.         wobject[i].num = 27 + bonus;
  433.         incscore (j, bonusscore[bonus]);
  434.         bonus++;
  435.         traptime[i] = 100;
  436.       }
  437.       else if (dying[j] == 0)               /* The ghost caught you */
  438.         dying[j] = DIESPEED;
  439.     }
  440.       }
  441.       else traptime[i]--;
  442.       if (traptime[i] == 1)
  443.       {
  444.     wobject[i].x = 192;
  445.     wobject[i].y = 144;
  446.     wobject[i].num = 19 + i;
  447.     bonus = 0;
  448.       }
  449.     }
  450.  
  451.     /* Find out the scrolling speed based on where you are in the window. */
  452.     if (wobject[0].x - worldx[WIN] < windowmaxx[WIN] / 2 - 1)
  453.       spx = -pacspeed;
  454.     else if (wobject[0].x - worldx[WIN] > windowmaxx[WIN] / 2 + 1)
  455.       spx = pacspeed;
  456.     
  457.     if (wobject[0].y - worldy[WIN] < windowmaxy[WIN] / 2 - 1)
  458.       spy = -pacspeed;
  459.     else if (wobject[0].y - worldy[WIN] > windowmaxy[WIN] / 2 + 1)
  460.       spy = pacspeed;
  461.   
  462.     nosound ();
  463.  
  464.     wscrollwindow (WIN, spx, spy);
  465.     wshowobjects (WIN, 0, 8, pacspr, wobject);
  466.     wputblock (0, 0, scrollblock[WIN], NORMAL);
  467.  
  468.     spx = 0;
  469.     spy = 0;
  470.     /* Do the same for the second player */
  471.     if (wobject[1].x - worldx[WIN2] < windowmaxx[WIN2] / 2 - 1)
  472.       spx = -pacspeed;
  473.     else if (wobject[1].x - worldx[WIN2] > windowmaxx[WIN2] / 2 + 1)
  474.     spx = pacspeed;
  475.     if (wobject[1].y - worldy[WIN2] < windowmaxy[WIN2] / 2 - 1)
  476.       spy = -pacspeed;
  477.     else if (wobject[1].y - worldy[WIN2] > windowmaxy[WIN2] / 2 + 1)
  478.     spy = pacspeed;
  479.     wscrollwindow (WIN2, spx, spy);
  480.     wshowobjects (WIN2, 0, 8, pacspr, wobject);
  481.     wretrace ();
  482.     wputblock (161, 0, scrollblock[WIN2], NORMAL);
  483.     frames++;
  484.   } while (!kbdon[ESC]);        /* until ESC is pressed */
  485.   wstoptimer ();
  486.   wdonetimer ();
  487.  
  488.   uninstallkbd ();
  489.   wendscroll (WIN);
  490.   wendscroll (WIN2);
  491.   wfreesprites (pactile, 0, NUM_LOAD);
  492.   wfreesprites (pacspr, 0, NUM_LOAD);
  493.   wfreemap (pacmap);
  494.   wsetmode (oldmode);
  495.  
  496.   printf ("Number of Frames: %i\n", frames);
  497.   printf ("Time elapsed:     %i microseconds\n", timer);
  498.   printf ("Frame Rate:       %f\n", (float)frames / (float)(timer / 100.0));
  499. }
  500.  
  501.